package cqrtplm.listener;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import com.ty.basic.session.RequestContext;
import cqrtplm.service.CountersignSynergyApproverService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;

import java.math.BigInteger;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : pieStar
 * @create 2025/7/18 22:40
 */
@Slf4j
@CustomListener(category = "润通", description = "设计规格变更会签审批人对应协同节点审批人")
public class RTCountersignSynergyApproverListener implements ExecutionListener {
    @Override
    public void notify(DelegateExecution execution) {
        long start = System.currentTimeMillis();
        String workFlowContainerId = execution.getProcessInstanceBusinessKey();
        log.info("----------润通----设计规格变更会签审批人对应协同节点审批人------开始------workFlowContainerId:{}----->>>",
                workFlowContainerId);
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = execution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        String finalWorkFlowContainerId = workFlowContainerId;
        Map<String, Object> requestContext = RequestContext.currentContext();
        CountersignSynergyApproverService countersignSynergyApproverService = SpringUtil.getBean(CountersignSynergyApproverService.class);
        CompletableFuture.runAsync(() -> {
            RequestContext.attachContext(requestContext);
            try {
                countersignSynergyApproverService.setTheSynergyApprover(new BigInteger(finalWorkFlowContainerId));
            } catch (Exception e) {
                log.info("编码主数据下发SAP");
                log.error(ExceptionUtil.getMessage(e));
                log.error(ExceptionUtil.stacktraceToString(e, 100000));
            }
        });
        log.info("----------润通----设计规格变更会签审批人对应协同节点审批人,耗时：{}---->>>", System.currentTimeMillis() - start);
    }
}
